package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private SetmealDishService setmealDishService;


    @PostMapping
    @CacheEvict(value = "SETMEAL_CACHE",key = "'SETMEAL'+ #setmealDto.status +'_'+#setmealDto.categoryId")
    public R add(@RequestBody SetmealDto setmealDto){
        setmealService.addSetmeal(setmealDto);
        return R.success("添加成功.");
    }

    /**
     * 分页查询套餐
     */
    @GetMapping("/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",required = true),
            @ApiImplicitParam(name = "pageSize",value = "每页记录数",required = true),
            @ApiImplicitParam(name = "name",value = "套餐名称",required = false)
    })
    public R findByPage(Integer page,Integer pageSize,String name){

        Page<SetmealDto> lists = setmealService.findByPage(page,pageSize,name);

        return R.success(lists);
    }

    /**
     * 批量删除套餐 -- 逻辑删除
     *  逻辑删除  update  ---  在数据库表中添加一个字段代表删除了
     *  物理删除   ---  数据库删除操作
     *  allEntries = true 清楚SETMEAL_CACHE 下的所有键  清空缓存
     */
    @DeleteMapping
    @CacheEvict(value = "SETMEAL_CACHE",allEntries = true)
    public R deleteIds(Long[] ids){
        setmealService.deleteIds(ids);
        return R.success("删除成功");
    }


    @Cacheable(value = "SETMEAL_CACHE",key = "'SETMEAL'+#setmeal.status +'_'+#setmeal.categoryId")
    @GetMapping("/list")
    public R findList(Setmeal setmeal){
        List<Setmeal> setmeals = setmealService.findList(setmeal);
        return R.success(setmeals);
    }
}